package org.aldar.bquest.entity.behaviour;

import org.aldar.bquest.constants.TileState;
import org.aldar.bquest.entity.AbilityTileSprite;
import org.aldar.bquest.entity.TileSprite;
import org.aldar.bquest.logic.controller.board.AfterMatchProcessor;
import org.aldar.bquest.logic.controller.entity.TileBoard;
import org.aldar.bquest.logic.controller.level.LevelController;
import org.aldar.bquest.util.Logger;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.util.modifier.IModifier;

/**
 * User: Daria
 * Date: 1/26/14 11:23 PM
 */
public class AfterMatchEntityModifierListener implements IEntityModifier.IEntityModifierListener {

    private String currentKey;
    private TileSprite tile;
    private Runnable extraAfterMatchAction;

    private static final Logger LOG = new Logger(AfterMatchEntityModifierListener.class);


    public AfterMatchEntityModifierListener(String currentKey, TileSprite tile) {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        LOG.i("Initialized AfterMatchEntityModifierListener for tile with key = " + currentKey + ", caller: " + stackTraceElements[4].getClassName() +
                "." + stackTraceElements[4].getMethodName() +
                " line " + stackTraceElements[4].getLineNumber());

        this.currentKey = currentKey;
        this.tile = tile;
    }

    public AfterMatchEntityModifierListener(String currentKey, AbilityTileSprite abilityTileSprite, Runnable extraAction) {
        this(currentKey, abilityTileSprite);
        this.extraAfterMatchAction = extraAction;
    }

    @Override
    public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
        TileBoard.tilesInAction.add(currentKey);
        LOG.i("Started AfterMatchEntityModifierListener for tile with key = " + currentKey);
    }

    @Override
    public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
        LOG.i("Finishing AfterMatchEntityModifierListener for tile with key = " + currentKey);

        if (extraAfterMatchAction != null) {
            LOG.i("Running extraAfterMatchAction for tile with key = " + currentKey);
            extraAfterMatchAction.run();
        }
        tile.setState(TileState.STATE_FALL_FINISH);
        TileBoard.tilesInAction.remove(currentKey);
        AfterMatchProcessor.killedKeys.remove(currentKey);
        if (TileBoard.tilesInAction.size() == 0 && AfterMatchProcessor.killedKeys.size() == 0) {
            if (TileBoard.getCrystalTileKeys().size() > 0 && !(LevelController.getCurrentLevelState() == LevelController.LevelState.FINISHED)) {
                TileBoard.attachMagicTile();
            }
            AfterMatchProcessor.fillEmpty();
        } else {
            LOG.i("Tiles in Action = " + TileBoard.tilesInAction);
            LOG.i("Killed keys = " + AfterMatchProcessor.killedKeys);
            LOG.i("Some tiles are still in action - modifier finish skip for tile = " + currentKey);
        }
    }


}
